<template>
    <el-container>
        <el-header style="border-bottom: none;">
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'collection.add'" @click="add"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'collection.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
                <el-button type="success" plainv-auth="'collection.check'" :disabled="selection.length == 0" @click="batch_check">审核</el-button>
	            <sc-export-button :auth="'collection.export'" :apiObj="apiObj" :search="search"></sc-export-button>
	            <sc-print-button :auth="'collection.print'" :selection="selection"></sc-print-button>
                <div class="finish">
                    <div class="finishNum">收款总额:<div class="text">{{total_money}}</div></div>
                </div>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-date-picker v-model="search.business_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-select v-model="search.customer_id" placeholder="请选择客户" filterable clearable>
                        <el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-tabs v-model="statusType" @tab-change="statusChange" style="padding-left: 20px;">
            <el-tab-pane label="已审核" name="2"></el-tab-pane>
            <el-tab-pane label="待审核" name="1"></el-tab-pane>
            <el-tab-pane label="全部" name="0"></el-tab-pane>
        </el-tabs>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border @setExtend="setExtend" :params="query">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                <el-table-column label="收款单号" prop="collect_code" align="center"></el-table-column>
                <el-table-column label="单据日期" prop="business_date" align="center"></el-table-column>
                <el-table-column label="客户名称" prop="customer_name" align="center" sortable>
                    <template #default="scope">
                        {{scope.row.customer.customer_name}}
                    </template>
                </el-table-column>
                <el-table-column label="外币" prop="foreign_currency" align="center"></el-table-column>
                <el-table-column label="汇率" prop="exchange_rate" align="center"></el-table-column>
                <!-- <el-table-column label="联系人" prop="customer.contacts" align="center"></el-table-column>
                <el-table-column label="联系电话" prop="customer.mobile" align="center"></el-table-column> -->
                <el-table-column label="收款金额" prop="money" align="center"></el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="审核状态" prop="status" align="center">
                    <template #default="scope">
                        <el-tag :type="scope.row.status == 1 ? 'warning' : 'success'">
                            {{scope.row.status == 1 ? '待审核' : '已审核'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="170">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'collection.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'collection.edit'" v-if="scope.row.status == 1">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'collection.delete'" v-if="scope.row.status == 1">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false"></check-status>
</template>

<script>
import CheckStatus from '../checkStatus.vue'
import saveDialog from './save'

export default {
	name: '客户列表',
	components: {
		saveDialog,CheckStatus,
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
			},
			apiObj: this.$API.finance.shouorder.list,
			selection: [],
			search: {
				customer_id: null,
                business_date:null,
			},
            query: {
                status: 2
            },
            total_money: "",
            statusType: "2",
            clientList:[],
		}
	},
    mounted() {
        this.getClient()
    },
	methods: {
        setExtend(data){
            this.total_money = data
        },
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row, index) {
			var reqData = { id: row.id }
			var res = await this.$API.finance.shouorder.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		async table_del_all(delete_ids) {
			var reqData = { id: delete_ids }
			var res = await this.$API.finance.shouorder.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
        batch_check(){
            var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
            this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('shou').setData(ids)
			})
        },
        async batch_uncheck(){
            var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
            var reqData = { id: ids,statsu:1 }
			var res = await this.$API.finance.shouorder.uncheck.post(reqData);
			if (res.code == 1) {
                this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
        },
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'shou') {
				this.$refs.table.refresh()
			}
		},
        // 客户列表
        async getClient(){
            var res = await this.$API.client.client.all.get();
            this.clientList = res.data
        },
        statusChange(e){
            if(e != 0){
                this.query.status = e
            }else{
                this.query.status = null
            }
            this.upsearch()
        },
	}
}
</script>

<style></style>
